<?php

/**
 * ECSHOP action文件
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */
$cache_id = sprintf('%X', crc32($_GET['c'] . '-' . $_CFG['lang']));
if(!empty($_GET['c']))
{
	$template = '';
	if($_GET['c']=='series')
	{
		$smarty->assign('mycategory',get_categories(1)); // 分类树
		$template = 'series.dwt';
	}
	else if($_GET['c']=='brand')
	{
		$template = 'brand.dwt';
	}
	else if($_GET['c']=='service')
	{
		$template = 'service.dwt';
	}
	else if($_GET['c']=='job')
	{
		$template = 'job.dwt';
	}
	else if($_GET['c']=='statement')
	{
		$template = 'statement.dwt';
	}
	else if($_GET['c']=='thanks')
	{
		$template = 'thanks.dwt';
	}
	else if($_GET['c']=='usage')
	{
		$template = 'usage.dwt';
	}
	else if($_GET['c']=='contact')
	{
		$template = 'contact.dwt';
	}
	//错误控制器跳转到指定页面
	else
	{
		ecs_header("Location:$Loaction\n");
	}
	//给模板控制器赋值
	$smarty->assign('c',$_GET['c']);
	if (!$smarty->is_cached($template, $cache_id))
    {
		assign_template();
	}
	$smarty->display($template, $cache_id);
}
//空控制器跳转到指定页面
else
{
	ecs_header("Location:$Loaction\n");
}

/*------------------------------------------------------ */
//-- PRIVATE FUNCTIONS
/*------------------------------------------------------ */
function get_categories($parent_id = 1)
{
	
    /*
     判断当前分类中全是是否是底级分类，
     如果是取出底级分类上级分类，
     如果不是取当前分类及其下的子分类
    */
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
    if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
    {
        /* 获取当前分类及其子分类 */
        $sql = 'SELECT cat_id,cat_name ,parent_id,is_show ' .
                'FROM ' . $GLOBALS['ecs']->table('category') .
                "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";

        $res = $GLOBALS['db']->getAll($sql);

        foreach ($res AS $row)
        {
            if ($row['is_show'])
            {
                $cat_arr[$row['cat_id']]['id']   = $row['cat_id'];
                $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
                $cat_arr[$row['cat_id']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

                if (isset($row['cat_id']) != NULL)
                {
                    $cat_arr[$row['cat_id']]['cat_id'] = get_child_category($row['cat_id']);
                }
            }
        }
    }
    if(isset($cat_arr))
    {
        return $cat_arr;
    }
}

function get_child_category($tree_id = 0)
{
    $three_arr = array();
    $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$tree_id' AND is_show = 1 ";
    if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
    {
        $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
                'FROM ' . $GLOBALS['ecs']->table('category') .
                "WHERE parent_id = '$tree_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
        $res = $GLOBALS['db']->getAll($child_sql);
        foreach ($res AS $row)
        {
            if ($row['is_show'])
			{

               $three_arr[$row['cat_id']]['id']   = $row['cat_id'];
               $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
               $three_arr[$row['cat_id']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

				if(isset($row['cat_id']) != NULL)
				{
					$three_arr[$row['cat_id']]['cat_id'] = get_child_category($row['cat_id']);
				}
				$three_arr[$row['cat_id']]['goods'] = get_goods($row['cat_id']);
			}
        }
    }
    return $three_arr;
}

//获取分类下的商品
function get_goods($cat_id)
{
	$sql = 'SELECT goods_id,goods_name FROM '.$GLOBALS['ecs']->table('goods').' WHERE cat_id = '.$cat_id.' LIMIT 0,4';
	$res = $GLOBALS['db']->GetAll($sql);
	$goods_arr = array();
	foreach($res as $row)
	{
		$goods_arr[$row['goods_id']]['url']  = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
		$goods_arr[$row['goods_id']]['goods_name'] = $row['goods_name'];
	}
	return $goods_arr;
}
?>